package {{.pkg}}

import (
	"context"
	"fmt"
	"encoding/json"

	"orchestrator/internal/infra/filter"
	"orchestrator/internal/infra/database/mysql/po"

	"github.com/osamingo/jsonrpc/v2"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/logc"
)

type {{.upperStartCamelObject}}FindOneParams struct {
	{{.upperStartCamelPrimaryKey}} {{.dataType}}
	*TxID
}

type {{.upperStartCamelObject}}FindOneResult struct {
	*{{.upperStartCamelObject}}
}

type {{.upperStartCamelObject}}FindOneFunc struct {
	Db sqlx.SqlConn
	Repo po.{{.upperStartCamelObject}}Model
	{{.upperStartCamelObject}}FindOneParams
	{{.upperStartCamelObject}}FindOneResult
}

func (t *{{.upperStartCamelObject}}FindOneFunc) Name() string {
	return fmt.Sprintf("%s.%s", "{{.table}}", "FindOne")
}

func (t *{{.upperStartCamelObject}}FindOneFunc) ServeJSONRPC(c context.Context, params *json.RawMessage) (result any, err *jsonrpc.Error) {
	logc.Infof(c, "%s( %s )", t.Name(), *params)
	m := makeMockData()
	r := {{.upperStartCamelObject}}FindOneResult{m}
	return r, nil
}

func (t *{{.upperStartCamelObject}}FindOneFunc) Params() any {
	return t.{{.upperStartCamelObject}}FindOneParams
}

func (t *{{.upperStartCamelObject}}FindOneFunc) Result() any {
	return t.{{.upperStartCamelObject}}FindOneResult
}

type TxID struct {
	TxID int `json:"TxID"`
}

type {{.upperStartCamelObject}}ListParams struct {
	*filter.QueryRequest
	*TxID
}

type {{.upperStartCamelObject}}ListResult struct {
	Total  int        `json:"total"`
	List   []*{{.upperStartCamelObject}} `json:"list"`
}

type {{.upperStartCamelObject}}ListFunc struct {
	Db sqlx.SqlConn
	Repo po.{{.upperStartCamelObject}}Model
	{{.upperStartCamelObject}}ListParams
	{{.upperStartCamelObject}}ListResult
}

func (t *{{.upperStartCamelObject}}ListFunc) Name() string {
	return fmt.Sprintf("%s.%s", "{{.table}}", "List")
}

func (t *{{.upperStartCamelObject}}ListFunc) ServeJSONRPC(c context.Context, params *json.RawMessage) (result any, err *jsonrpc.Error) {
	logc.Infof(c, "%s( %s )", t.Name(), *params)

	d := filter.NewAndFilter()
	p := filter.NewQueryRequest(d, &filter.Group{}, []*filter.Sort{}, filter.NewPagination(0, 10))

	pl := []{{.upperStartCamelObject}}ListParams{}
	ppp := {{.upperStartCamelObject}}ListParams{QueryRequest: p}
	tx := {{.upperStartCamelObject}}ListParams{TxID: &TxID{}}
	pl = append(pl, ppp)
	pl = append(pl, tx)

	err1 := json.Unmarshal(*params, &pl)
	if err1 != nil {
		return nil, &jsonrpc.Error{
			Code:    jsonrpc.ErrorCodeInvalidRequest,
			Message: err1.Error(),
		}
	}

	// 使用 json.MarshalIndent() 函数格式化输出
	pj, err1 := json.MarshalIndent(pl, "", "  ")
	if err1 != nil {
		fmt.Println("Error:", err1)
		return nil, &jsonrpc.Error{
			Code:    jsonrpc.ErrorCodeInvalidRequest,
			Message: err1.Error(),
		}
	}

	fmt.Printf("%s:  %s\n", t.Name(), pj)

	m := []*{{.upperStartCamelObject}}{makeMockData()}
	r := {{.upperStartCamelObject}}ListResult{
		    Total: len(m),
		  	List: m,
	}
	return r, nil
}

func (t *{{.upperStartCamelObject}}ListFunc) Params() any {
	return t.{{.upperStartCamelObject}}ListParams
}

func (t *{{.upperStartCamelObject}}ListFunc) Result() any {
	return t.{{.upperStartCamelObject}}ListResult
}
